<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>柱状图</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        margin: 20px;
      }

      .chart-container {
        position: relative;
        width: 600px;
        overflow-x: scroll;
        border: 1px solid #ddd;
        padding: 10px;
      }

      .chart {
        display: flex;
        align-items: flex-end;
        height: 300px;
        width: 1000px;
        position: relative;
      }

      .bar {
        margin: 0 10px;
        position: relative;
      }

      .bar div {
        width: 30px;
        background-color: steelblue;
        margin-bottom: 5px;
        transform: scaleY(0);
        transform-origin: bottom;
        animation: grow 1s ease-out forwards;
      }

      .bar div.good {
        background-color: lightblue;
      }

      .tooltip {
        position: absolute;
        background: rgba(0, 0, 0, 0.7);
        color: white;
        padding: 5px;
        border-radius: 3px;
        font-size: 12px;
        pointer-events: none;
        opacity: 0;
        transition: opacity 0.3s;
      }

      .legend {
        margin-top: 10px;
        display: flex;
        gap: 10px;
      }

      .legend div {
        display: flex;
        align-items: center;
      }

      .legend div span {
        width: 20px;
        height: 20px;
        display: inline-block;
        margin-right: 5px;
      }

      .legend .excellent span {
        background-color: steelblue;
      }

      .legend .good span {
        background-color: lightblue;
      }

      @keyframes grow {
        from {
          transform: scaleY(0);
        }

        to {
          transform: scaleY(1);
        }
      }
    </style>
  </head>

  <body>
    <h1>领导班子综合考核评价</h1>
    <div class="chart-container">
      <div class="chart" id="chart"></div>
    </div>
    <div class="legend">
      <div class="excellent"><span></span>优</div>
      <div class="good"><span></span>良</div>
    </div>
    <div class="tooltip" id="tooltip"></div>

    <script>
      // 数据配置
      const data = [
        {
          year: 2020,
          groups: [
            { name: "001", value: 50, type: "good" },
            { name: "002", value: 30, type: "excellent" },
            { name: "003", value: 40, type: "good" },
          ],
        },
        {
          year: 2021,
          groups: [
            { name: "001", value: 60, type: "excellent" },
            { name: "002", value: 20, type: "good" },
            { name: "003", value: 50, type: "excellent" },
          ],
        },
        {
          year: 2022,
          groups: [
            { name: "001", value: 70, type: "excellent" },
            { name: "002", value: 50, type: "good" },
            { name: "003", value: 60, type: "excellent" },
          ],
        },
        {
          year: 2023,
          groups: [
            { name: "001", value: 80, type: "excellent" },
            { name: "002", value: 40, type: "good" },
            { name: "003", value: 70, type: "excellent" },
          ],
        },
        {
          year: 2024,
          groups: [
            { name: "001", value: 90, type: "excellent" },
            { name: "002", value: 60, type: "good" },
            { name: "003", value: 80, type: "excellent" },
          ],
        },
      ];

      const chart = document.getElementById("chart");
      const tooltip = document.getElementById("tooltip");

      // 渲染图表
      data.forEach((item) => {
        const yearGroup = document.createElement("div");
        yearGroup.className = "bar";

        item.groups.forEach((group) => {
          const bar = document.createElement("div");
          bar.style.height = `${group.value * 2}px`;
          bar.className = group.type;
          bar.dataset.tooltip = `${group.name}: ${group.value}`;

          bar.addEventListener("mouseover", (e) => {
            tooltip.style.opacity = 1;
            tooltip.textContent = e.target.dataset.tooltip;
            tooltip.style.left = `${e.pageX + 10}px`;
            tooltip.style.top = `${e.pageY + 10}px`;
          });

          bar.addEventListener("mouseout", () => {
            tooltip.style.opacity = 0;
          });

          yearGroup.appendChild(bar);
        });

        chart.appendChild(yearGroup);
      });
    </script>
  </body>
</html>
